local BaseData = require("data.base.BaseData")
local GameLogicData = class("GameLogicData", BaseData)

local schema = {}

GameLogicData.schema = schema

function GameLogicData:ctor()
    GameLogicData.super.ctor(self)
    self._signalId = G_NetWorkManager:beginCache()
    G_NetWorkManager:add(MessageIDConst.ID_S2C_MonsterNotify, handler(self, self._s2cMonsterNotify))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_MonsterLeaveView, handler(self, self._s2cMonsterLeaveView))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_MonsterTrack, handler(self, self._s2cMonsterTrack))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_MonsterFight, handler(self, self._s2cMonsterFight))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_LeaveMonsterTrack, handler(self, self._s2cLeaveMonsterTrack))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_MonsterLeaveFight, handler(self, self._s2cMonsterLeaveFight))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_NotifyMonsterStatus, handler(self, self._s2cNotifyMonsterStatus))
    G_NetWorkManager:endCache()
end

function GameLogicData:clear()
    G_NetWorkManager:removeCache(self._signalId)
end

function GameLogicData:c2sMonsterTrack(sceneId, monsterId)
    local myId = G_UserData:getBase():getId()
    G_NetWorkManager:send(
        MessageIDConst.ID_C2S_MonsterTrack,
        {
            sceneId = sceneId,
            monsterId = monsterId,
            trackId = myId
        }
    )
end

function GameLogicData:_fightMonster(sceneId, monsterId)
    local fightId = G_UserData:getBase():getId()
    G_SignalManager:dispatch(SignalConst.EVENT_MONSTER_FIGHT_PLAYER, sceneId, monsterId, fightId)
end

function GameLogicData:c2sMonsterFight(sceneId, monsterId)
    if not G_SocketManager:isContenting() then
        self:_fightMonster(sceneId, monsterId)
        return
    end
    local myId = G_UserData:getBase():getId()
    G_NetWorkManager:send(
        MessageIDConst.ID_C2S_MonsterFight,
        {
            sceneId = sceneId,
            monsterId = monsterId,
            fightId = myId
        }
    )
end

function GameLogicData:_fightMonsterLeave(sceneId, monsterId, success)
    local status = 1
    G_SignalManager:dispatch(SignalConst.EVENT_MONSTER_STATUS_NOTIFY, sceneId, monsterId, status, topos)
end

function GameLogicData:c2sMonsterLeaveFight(sceneId, monsterId, success)
    if not G_SocketManager:isContenting() then
        self:_fightMonsterLeave(sceneId, monsterId, success)
        return
    end
    G_NetWorkManager:send(
        MessageIDConst.ID_C2S_MonsterLeaveFight,
        {
            sceneId = sceneId,
            monsterId = monsterId,
            success = success
        }
    )
end

function GameLogicData:_s2cMonsterNotify(id, message)
    -- int32 sceneId= 1;
    -- int32 monsterId= 2;
    -- vec3 pos=3;
    local sceneId = rawget(message, "sceneId")
    local monsterId = rawget(message, "monsterId")
    local configId = rawget(message, "configId")
    local pos = rawget(message, "pos")
    local isFight = rawget(message, "isFight")
    printDebug("UserBaseData:_s2cMonsterNotify " .. monsterId)
    G_SignalManager:dispatch(SignalConst.EVENT_SCENE_NOTIFY_MONSTER, sceneId, monsterId, pos, isFight)
end

function GameLogicData:_s2cMonsterLeaveView(id, message)
    --     int32 sceneId =1;
    --   int32 monsterId =2;
    local sceneId = rawget(message, "sceneId")
    local monsterId = rawget(message, "monsterId")
    printDebug("UserBaseData:_s2cMonsterLeaveView " .. monsterId)
    G_SignalManager:dispatch(SignalConst.EVENT_SCENE_MONSTER_LEAVE_VIEW, sceneId, monsterId)
end

function GameLogicData:_s2cMonsterTrack(id, message)
    if message.ret ~= 1 then
        return
    end
    -- int32 sceneId =2;
    -- int32 monsterId = 3;
    -- int32 trackId=4;
    local sceneId = rawget(message, "sceneId")
    local monsterId = rawget(message, "monsterId")
    local trackId = rawget(message, "trackId")
    G_SignalManager:dispatch(SignalConst.EVENT_MONSTER_TRACK_PLAYER, sceneId, monsterId, trackId)
end

function GameLogicData:_s2cMonsterFight(id, message)
    if message.ret ~= 1 then
        return
    end
    local sceneId = rawget(message, "sceneId")
    local monsterId = rawget(message, "monsterId")
    local fightId = rawget(message, "fightId")
    G_SignalManager:dispatch(SignalConst.EVENT_MONSTER_FIGHT_PLAYER, sceneId, monsterId, fightId)
end

function GameLogicData:_s2cLeaveMonsterTrack(id, message)
    -- int32 sceneId =1;
    -- int32 monsterId =2;
    -- int32 trackId= 3;
    local sceneId = rawget(message, "sceneId")
    local monsterId = rawget(message, "monsterId")
    local trackId = rawget(message, "trackId")
    G_SignalManager:dispatch(SignalConst.EVENT_LEAVE_MONSTER_TRACK, sceneId, monsterId, trackId)
end

function GameLogicData:_s2cMonsterLeaveFight()
end

function GameLogicData:_s2cNotifyMonsterStatus(id, message)
    local sceneId = rawget(message, "sceneId")
    local monsterId = rawget(message, "monsterId")
    local status = rawget(message, "status") -- 1-正常状态 2-战斗状态 3-死亡状态
    local topos = rawget(message, "topos")
    printDebug(" GameLogicData:_s2cNotifyMonsterStatus " .. sceneId .. " " .. monsterId .. " " .. status)
    G_SignalManager:dispatch(SignalConst.EVENT_MONSTER_STATUS_NOTIFY, sceneId, monsterId, status, topos)
end

return GameLogicData
